﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TravelGenerator.BusinessObjects.SavingManagement;
using TravelGenerator.BusinessObjects.ObjectValidator;
using TravelGenerator.BusinessObjects.ModelObjects;
using TravelGenerator.DataAccessLayer;
using TravelGenerator.IBusinessLayer.TravelManagement;

namespace TravelGenerator.BusinessLayer.TravelManagement
{
    internal abstract class BLTravelManagement
    {
        #region Properties

        private TravelGeneratorEntities contextManager;
        /// <summary>
        /// Gets or sets the context.
        /// </summary>
        /// <value>The context.</value>
        protected TravelGeneratorEntities ContextManager
        {
            get
            {
                return this.contextManager;
            }
            set
            {
                this.contextManager = value;
            }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="BLTravelManagement"/> class.
        /// </summary>
        /// <param name="blMaster">The bl master.</param>
        protected BLTravelManagement(BLTravelManagement blMaster = null)
        {
            if (blMaster == null)
            {
                this.ContextManager = new TravelGeneratorEntities();
            }
            else
            {
                this.ContextManager = blMaster.ContextManager;
            }
        }

        #endregion

        /// <summary>
        /// Methods allows you to get the saving parameter
        /// </summary>
        /// <param name="entityToSave">The entity to save.</param>
        /// <param name="contact">The contact.</param>
        /// <returns></returns>
        public SaveParameter GetSaveParameter(IValidateEntity entityToSave, Contact contact)
        {
            SaveParameter parameter = SaveParameter.Create(entityToSave, contact);
            
            return parameter;
        }

        /// <summary>
        /// Méthode permettant de déterminer s'il est possible de sauvegarder l'élément ou pas.
        /// </summary>
        /// <param name="parameter">The parameter.</param>
        /// <returns></returns>
        public SaveResult GetSaveResult(SaveParameter parameter)
        {
            SaveResult saveResult = SaveResult.Create(parameter);

            //Vérification si le contact est connecté
            if (parameter.Contact == null)
                saveResult.SaveError = SaveError.NotConnected;
            else
            {
                //Vérification de la validité de l'entité
                saveResult.CheckResultCollection = parameter.EntityToSave.CheckElement();
                if (!saveResult.CheckResultCollection.IsValid)                
                    saveResult.SaveError = SaveError.EntityNotValid;                                    
                else                
                    //Aucune erreur n'a été détectée
                    saveResult.SaveError = SaveError.CanSave;                
            }

            return saveResult;
        }

        /// <summary>
        /// Saves the entity.
        /// </summary>
        /// <param name="result">The result.</param>
        /// <returns></returns>
        public SaveEntityResult SaveEntity(SaveResult result)
        {
            SaveEntityResult save = SaveEntityResult.Create(result);

            try
            {
                this.ContextManager.SaveChanges();
            }
            catch (Exception ex)
            {
                save.Error = ex;
            }

            return save;
        }
    }
}
